<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui">
    <head>
        <p:resources />
    </head>
    <body>
        <ui:composition template="./../Template/generalTemplate.xhtml">
            <ui:define name="content">
                <p:panel header="Pocess New Purchase Order" id ="purchaseOrder" style="height:495px;">

                    <h:form id="salesForm" prependId="false"> 
                        <p:panel>
                            <div style="margin-top:0px;margin-left:0px;margin-right:0px;overflow:auto;height:400px;margin-bottom:0px;">
                                <p:panel id="salesQuotation" header="SalesQuotation Information" toggleable="true" >

                                 
                                        <h:outputText value="Company:" />  
                                        <h:outputText value="#{createNewPurchaseOrderManagedBean.salesQuotation.company.name}" />
                                  
                                    <p:dataTable var="lineItem" value="#{createNewPurchaseOrderManagedBean.salesQuotation.quotationLineItems}" dynamic="true" emptyMessage="No product selected">
                                        <p:column>
                                            <f:facet name="header">
                                                <h:outputText value="Proudct" />
                                            </f:facet>
                                            <h:outputText value="#{lineItem.product.productName}" />
                                        </p:column>
                                        <p:column>
                                            <f:facet name="header">
                                                <h:outputText value="Quantity (in cases)" />
                                            </f:facet>
                                            <h:outputText value="#{lineItem.quantity}" />
                                        </p:column>
                                        <p:column>
                                            <f:facet name="header">
                                                <h:outputText value="Unit Price" />
                                            </f:facet>
                                            <h:outputText value="#{lineItem.price}" />
                                        </p:column>
                                    </p:dataTable>

                                    <h:outputText value="User Discount"/><p:spacer width="10" />
                                    <h:outputText value="#{createNewPurchaseOrderManagedBean.salesQuotation.userDiscount}"/>
                                   


                                </p:panel>
                                <p:panel  id="salesOrderLineItems"  header="Purchase Order LineItem List" toggleable="true" >                        
                                    <p:dataTable  id ="lineItemList" dynamic="true" emptyMessage="No product selected" value="#{createNewPurchaseOrderManagedBean.lineItemList}" var="o" paginator="true" rows="2">

                                        <p:column>
                                            <f:facet name="header">Product Name</f:facet>
                                            #{o.product.productName}
                                        </p:column> 

                                        <p:column>
                                            <f:facet name="header">Quantity</f:facet>
                                            #{o.quantity}
                                        </p:column>

                                        <p:column>
                                            <f:facet name="header">Delivery Date</f:facet>
                                            <h:outputText value="#{o.deliveryDate}">
                                                <f:convertDateTime pattern ="dd/MM/yyyy"/>
                                            </h:outputText>    
                                        </p:column>

                                        <p:column>
                                            <f:facet name="header">Delivery Location</f:facet>
                                            #{o.deliveryLocation}
                                        </p:column> 

                                        <p:column>
                                            <f:facet name="header">Unit Price</f:facet>
                                            #{o.price}
                                        </p:column>

                                        <p:column>
                                            <f:facet name="header">Cum Amount</f:facet>
                                            #{o.cumAmount}
                                        </p:column>

                                        <p:column>
                                            <f:facet name="header">Edit</f:facet>
                                            <h:commandLink value =" Delete" action="#{createNewPurchaseOrderManagedBean.deleteLineItem(o)}"/>
                                        </p:column>        
                                    </p:dataTable>
                                </p:panel>





                                <h:panelGrid id="grid" columns="3">
                                    <h:outputText value="ChooseProduct: "/>
                                    <h:selectOneMenu id="productType" value="#{createNewPurchaseOrderManagedBean.productId}" required="true">  

                                        <f:selectItems value="#{createNewPurchaseOrderManagedBean.productList}" />
                                    </h:selectOneMenu>
                                    <p:message for="productType"/>

                                    <h:outputText value = "Quantity"/>
                                    <h:inputText id="a" value="#{createNewPurchaseOrderManagedBean.lineItem.quantity}" />
                                    <p:message for="a"/>

                                    <h:outputText value = "Delivery Date (dd/mm/yyyy)"/>
                                    <h:inputText id="b" value="#{createNewPurchaseOrderManagedBean.lineItem.deliveryDate}" >
                                        <f:convertDateTime pattern ="dd/MM/yyyy"/>
                                    </h:inputText>
                                    <p:message for="b"/>

                                    <h:outputText value = "Delivery Location"/>
                                    <h:inputText  id="c" value="#{createNewPurchaseOrderManagedBean.lineItem.deliveryLocation}" />
                                    <p:message for="c"/>

                                    <h:outputText value = "Unit Price"/>
                                    <h:outputText id="unitPrice" value="#{createNewPurchaseOrderManagedBean.lineItem.price}"/>
                                    <br />

                                    <h:outputText value = "Cum Amount"/>
                                    <h:outputText  id="cumAmount" value="#{createNewPurchaseOrderManagedBean.lineItem.cumAmount}" />

                                    <p:poll interval="2"   
                                            actionListener="#{createNewPurchaseOrderManagedBean.calculateCumAmount}" update="unitPrice cumAmount" />
                                    <p:commandButton value="Add" update="lineItemList" ajax ="false" actionListener ="#{createNewPurchaseOrderManagedBean.addLineItem}"/>
                                </h:panelGrid>


                            </div>
                        </p:panel>
                    </h:form>

                    <h:form >  
                        <p:notificationBar position="bottom" effect="slide" widgetVar="bottomBar" styleClass="bottom" >  
                            <f:facet name="close">  
                                <p:graphicImage value="/images/closebar.png" />  
                            </f:facet>  
                            <h:panelGroup id="bottomPanel" style="margin-right:200px;">  
                                <!--<p:messages />-->  
                                <h:outputText value="ERROR: #{createNewPurchaseOrderManagedBean.errorMessage}"  />  
                            </h:panelGroup>  
                        </p:notificationBar>


                        <p:spacer width="20"/>
                        <p:commandButton update="bottomPanel"  oncomplete="bottomBar.show();"  value="Create"  actionListener="#{createNewPurchaseOrderManagedBean.createNewPurchaseOrder}" />
                    </h:form>

                </p:panel>
            </ui:define>

        </ui:composition>

    </body>
</html>
